require 'helper'

describe VastApi do
  before do
    @vast = VastApi.new
  end

  describe 'Listings' do
    before do
      VCR.use_cassette('bike listings', :record => :new_episodes) do
        @listing = @vast.category('item_vehicle/bike').listings
      end
    end

    it "should return a list of items" do
      @listing.size.must_be(:>, 0)
    end

    it "should be able to get attributes from listings" do
      id = @listing.attr('id')
      id.wont_be_nil
      #id.should == @listing.attr('id')
    end

    it "should be able to get query options for listings" do
      @listing.query['category'].wont_be_nil
    end

    it "should be able to get options from listings" do
      @listing.attr('startIndex').wont_be_nil
    end

    it "should be ablt to get vars" do
      @listing.attr('site_count').wont_be_nil
    end

    it "should return an error if no category is set" do
      lambda { VastApi.new.listings }.must_raise ArgumentError
    end

    it "should be able to filter items" do
      VCR.use_cassette("filter items", :record => :new_episodes) do
        filter = @vast.category('horses').where('pet-gen'=>'Male').listings
        filter.size.must_be(:>, 0)
      end
    end

    it "find should raise an error if there is no category" do
      bike = @listing.first
      lambda { VastApi.new.find(bike.id) }.must_raise ArgumentError
    end

    it "find should get a single item" do
      bike = @listing.first
      find_bike_cat = ''
      VCR.use_cassette('find bike with category', :record => :new_episodes) do
        find_bike_cat = @listing.find!(bike.id)
      end
      find_bike_cat.id.must_equal bike.id
    end

    it "post lead submission" do
      bike = @listing.first
      params = {:comments => "No comments", :phone => "123123323", :street_1 => "134 W 29 ST", :regioncode => "NY",
                :postalcode => "10001", :city => "New York", :email => "one@gmail.com", :name_first => "test", :name_last => "test"}
      VCR.use_cassette('post lead', :record => :new_episodes) do
        res = bike.post_leads(params)
        res[:status].to_i.must_be(:>, 0)
      end
    end

    it "get required fields" do
      bike = @listing.first
      VCR.use_cassette('get required fields', :record => :new_episodes) do
        res = bike.req_fields
        res.size.must_be(:==, 7)
      end
    end

    it "entry should have a hash of links" do
      bike = @listing.first
      bike.vast_links['via'].wont_be_nil
    end

  end

  describe 'Attributes' do
    before do
      VCR.use_cassette('find bike with attribute', :record => :new_episodes) do
        @attributes = @vast.category('item_vehicle/bike').attributes
      end
    end

    it "should be able to fetch attributes" do
      @attributes.size.must_be(:>, 0)
    end

    it "each entry should be hash" do
      @attributes.must_be_kind_of VastApi::Attributes
    end
  end

  describe 'listings with attributes' do

    before do
      VCR.use_cassette('bike listings', :record => :new_episodes) do
        @listing = @vast.category('item_vehicle/bike').where('include'=>'attributes').listings
      end
    end

    it "check if listings are all listings" do
      @listing.each do |l|
        l.must_be_kind_of VastApi::Listings::Entry
      end
    end

    it "check if attributes are there" do
      @listing.instance_variable_get(:@attributes).must_be_kind_of Hash
      @listing.instance_variable_get(:@attributes).values.each do |l|
        l.must_be_kind_of VastApi::Attributes::Entry
      end
    end

  end

  describe 'attributes with listings' do

    before do
      VCR.use_cassette('bike listings', :record => :new_episodes) do
        @attributes = @vast.category('item_vehicle/bike').where('include'=>'listings').attributes
      end
    end

    it "check if listings are all attributes" do
      @attributes.values.each do |l|
        l.must_be_kind_of VastApi::Attributes::Entry
      end
    end

    it "check if attributes are there" do
      @attributes.instance_variable_get(:@listings).must_be_kind_of Array
      @attributes.instance_variable_get(:@listings).each do |l|
        l.must_be_kind_of VastApi::Listings::Entry
      end
    end

  end


  describe 'Categories' do
    before do
      VCR.use_cassette('find bike with category', :record => :new_episodes) do
        @categories = @vast.categories
      end
    end

    it "should be a kind of category" do
      @categories.must_be_kind_of VastApi::Categories
    end

    it "should be able to fetch categories" do
      @categories.size.must_be(:>, 0)
    end
  end

  describe 'create_entry' do
    it "should detect this as an item" do
      node = Nokogiri::XML('
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns=\'http://www.w3.org/2005/Atom\' xmlns:o=\'http://a9.com/-/spec/opensearchrss/1.0/\' xmlns:v=\'http://data.vast.com/ns/listings\'>
<entry>
<id>http://data.vast.com/listings/4492261456855456117/-/item_vehicle/bike</id>
<updated>2011-08-08T09:05:26+02:00</updated>
<published>2011-08-08T09:05:27+02:00</published>
<author>
<name>www.sample.com</name>
</author>
<title type="text">SAMPLE - 1974 Womens Schwinn Varsity 10 Speed Bike All Original w Fenders Light...</title>
<v:vertical>general</v:vertical>
<v:category>item_vehicle%2Cbike</v:category>
<v:item_id>4492261456855456117</v:item_id>
<v:location precision="zip">
<v:state>TN</v:state>
<v:country>United States</v:country>
<v:zip>37132</v:zip>
<v:distance>0</v:distance>
<v:city>Murfreesboro</v:city>
<v:lat precision="unknown">35.847750</v:lat>
<v:lon precision="unknown">-86.363590</v:lon>
<v:address></v:address>
</v:location>
<v:price type="money" symbol="$" currency="USD" value="0"/>
<v:currency type="text">USD</v:currency>
<link rel="self" type="application/atom+xml" href="http://data.vast.com/listings/4492261456855456117/-/item_vehicle/bike?apikey="/>
<link rel="via" type="text/html" title="www.sample.com" href="http://details.vast.com/details/item_vehicle/bike/ob-NzezewthgbbXRMZttJ2JlJ27Hw==/"/>
<link rel="alternate" type="text/html" title="1974 Womens Schwinn Varsity 10 Speed Bike All Original w Fenders Light..." href="http://details.vast.com/details/item_vehicle/bike/ob-NzezewthgbbXRMZttJ2JlJ27Hw==/"/>
<link rel="http://feedback.vast.com" type="text/html" title="Report incorrect result" href="http://www.vast.com/ajax/report?id=4492261456855456117&amp;category=item_vehicle/bike&amp;apikey="/>
<v:domain_name type="text">www.sample.com</v:domain_name>
<v:year type="number"></v:year>
<v:image_url type="image/jpeg">http://img.vast.com/128x96/4492261456855456117</v:image_url>
<v:large_image_url type="image/jpeg">http://img.vast.com/320x240/4492261456855456117</v:large_image_url>
<v:tiny_image_url type="image/jpeg">http://img.vast.com/59x44/4492261456855456117</v:tiny_image_url>
<v:image_count>1</v:image_count>
<v:hasimage type="boolean">yes</v:hasimage>
<v:hosted type="boolean">no</v:hosted>
<v:hosted_type type="text">landing</v:hosted_type>
<v:description type="text">1974 Schwinn Varsity Sport. It is all original and in great shape just look at the pictures. It has metal fenders a working generator and working lights front and tail. The original paint and decals are in great shape as well as the leather seat. Also included the original owners manual that came with this bicycle.</v:description>
<content type="html">&lt;img src="http://img.vast.com/128x96/4492261456855456117"&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;description:&lt;/strong&gt;1974 Schwinn Varsity Sport. It is all original and in great shape just look at the pictures. It has metal fenders a working generator and working lights front and tail. The original paint and decals are in great shape as well as the leather seat. Also included the original owners manual that came with this bicycle.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;price:&lt;/strong&gt;0&lt;/li&gt;&lt;li&gt;&lt;strong&gt;country:&lt;/strong&gt;United States&lt;/li&gt;&lt;li&gt;&lt;strong&gt;state:&lt;/strong&gt;TN&lt;/li&gt;&lt;li&gt;&lt;strong&gt;zip:&lt;/strong&gt;37132&lt;/li&gt;&lt;li&gt;&lt;strong&gt;city:&lt;/strong&gt;Murfreesboro&lt;/li&gt;&lt;/ul&gt;</content>
</entry>
</feed>')
      @vast.create_entry(node.root.children[1]).must_be_kind_of VastApi::Listings::Entry
    end

    it "should detect this as an attribute" do
      node = Nokogiri::XML(%q|
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:o='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:v='http://data.vast.com/ns/listings'>
<entry>
<id>http://data.vast.com/attributes/location/-/item_vehicle/bike</id>
<title type='text'>SAMPLE - Location</title>
<updated>2011-08-12T15:40:14+02:00</updated>
<author>
<name>www.vast.com</name>
</author>
<v:attribute id="location" type="text">
<v:value id="Park-City--UT" count="108">Park City, UT</v:value>
<v:value id="New-York--NY" count="32">New York, NY</v:value>
<v:value id="Fullerton--CA" count="18">Fullerton, CA</v:value>
<v:value id="Los-Angeles--CA" count="18">Los Angeles, CA</v:value>
<v:value id="Birmingham--AL" count="15">Birmingham, AL</v:value>
<v:value id="Boulder--CO" count="8">Boulder, CO</v:value>
<v:value id="Salt-Lake-City--UT" count="8">Salt Lake City, UT</v:value>
<v:value id="Porter--TX" count="8">Porter, TX</v:value>
<v:value id="North-Bend--WA" count="7">North Bend, WA</v:value>
<v:value id="Seattle--WA" count="6">Seattle, WA</v:value>
<v:value id="Louisville--KY" count="6">Louisville, KY</v:value>
<v:value id="Orlando--FL" count="6">Orlando, FL</v:value>
<v:value id="Bridgeport--CT" count="6">Bridgeport, CT</v:value>
<v:value id="Milwaukee--WI" count="5">Milwaukee, WI</v:value>
<v:value id="San-Carlos--CA" count="5">San Carlos, CA</v:value>
<v:value id="Sherman--IL" count="5">Sherman, IL</v:value>
<v:value id="Northbrook--IL" count="5">Northbrook, IL</v:value>
<v:value id="Ventura--CA" count="5">Ventura, CA</v:value>
<v:value id="Trumbull--CT" count="5">Trumbull, CT</v:value>
<v:value id="Norwalk--CT" count="5">Norwalk, CT</v:value>
<v:value id="Washington--NY" count="5">Washington, NY</v:value>
<v:value id="Phoenix--AZ" count="4">Phoenix, AZ</v:value>
<v:value id="Denver--CO" count="4">Denver, CO</v:value>
<v:value id="New-Haven--CT" count="4">New Haven, CT</v:value>
<v:value id="Indianapolis--IN" count="4">Indianapolis, IN</v:value>
<v:value id="Surprise--AZ" count="4">Surprise, AZ</v:value>
<v:value id="Jacksonville--FL" count="4">Jacksonville, FL</v:value>
<v:value id="Colorado-Springs--CO" count="3">Colorado Springs, CO</v:value>
<v:value id="Portland--OR" count="3">Portland, OR</v:value>
<v:value id="Sandy-Hook--CT" count="3">Sandy Hook, CT</v:value>
<v:value id="Salem--OR" count="3">Salem, OR</v:value>
<v:value id="Reading--PA" count="3">Reading, PA</v:value>
<v:value id="Spokane--WA" count="3">Spokane, WA</v:value>
<v:value id="Rock-Hill--SC" count="3">Rock Hill, SC</v:value>
<v:value id="Waxahachie--TX" count="3">Waxahachie, TX</v:value>
<v:value id="Columbus--OH" count="3">Columbus, OH</v:value>
<v:value id="Sacramento--CA" count="3">Sacramento, CA</v:value>
<v:value id="Aurora--IL" count="3">Aurora, IL</v:value>
<v:value id="Bellingham--WA" count="3">Bellingham, WA</v:value>
<v:value id="North-Haven--CT" count="3">North Haven, CT</v:value>
<v:value id="Kingwood--TX" count="3">Kingwood, TX</v:value>
<v:value id="Atlanta--GA" count="3">Atlanta, GA</v:value>
<v:value id="Houston--TX" count="3">Houston, TX</v:value>
<v:value id="Ontario--CA" count="3">Ontario, CA</v:value>
<v:value id="Lincoln--NE" count="3">Lincoln, NE</v:value>
<v:value id="Other-CA" count="6">Other CA</v:value>
<v:value id="Other-PA" count="6">Other PA</v:value>
<v:value id="Other-IN" count="5">Other IN</v:value>
<v:value id="Other-NY" count="4">Other NY</v:value>
<v:value id="unknown" count="190">unknown</v:value>
</v:attribute>
</entry>
</feed>|)
      @vast.create_entry(node.root.children[1]).must_be_kind_of VastApi::Attributes::Entry
    end

  end
end

